﻿// -----------------------------------------------------------------------
// <copyright file="DFA.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Scanner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    struct Transition
    {
        public State FromState;
        public State ToState;
        public string type;
        public bool checkType(char c)
        {
            switch (type)
            {
                case "letter":
                    {
                        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                        {
                            return true;
                        }
                    }break;
                case "digit":
                    {
                        if (c >= '0' && c <= '9')
                        {
                            return true;
                        }
                    }break;

                case "other_id":
                    {
                        if (!(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'Z') &&! (c >= 'a' && c <= 'z'))
                        {
                            return true;
                        }
                    }break;
                case "other_num":
                    {
                        if (!(c >= '0' && c <= '9'))
                        {
                            return true;
                        }
                    } break;

                case "other_comment":
                    {
                        if (c != '/' && c != '*')
                        {
                            return true;
                        }
                    } break;
                case "whiteSpace":
                    {
                        if (c == '\n' || c == '\t' || c == ' ')
                        {
                            return true;
                        }
                    }break;
                default:
                    {
                        if (type[0] == c)
                            return true;
                        else
                            return false;
                    }break;

            } return false;
        }
    }

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class DFA
    {
        public DFA()
        {
            states = new State[50];
            transitions = new Transition[50];
        }
        private State[] states;

        public State[] States
        {
            get { return states; }
            set { states = value; }
        }
        private int startNodeIndex;

        public int StartNodeIndex
        {
            get { return startNodeIndex; }
            set { startNodeIndex = value; }
        }
        private Transition[] transitions;

        internal Transition[] Transitions
        {
            get { return transitions; }
            set { transitions = value; }
        }

        public State Translate(State fromState, char input)
        {
            for (int i = 0; i < this.transitions.GetLength(0); i++)
            {
                if (transitions[i].FromState == fromState)
                {
                    if (transitions[i].checkType(input))
                    {
                        return transitions[i].ToState;
                    }
                }
            }
            State errorState = new State("error");
            errorState.IsError = true;
            return errorState;
        }
        
    }
}
